

import requests

from urllib.parse import quote


poet_name = "财经早餐"
text = quote(poet_name, 'utf-8')

def search_org(org_name,useragent,token,cookies_str):
    if isinstance(org_name,str):
        url_encode_str = quote(org_name, 'utf-8')
    else:
        raise Exception("传入的公众号名字必须是字符串类型的")
    urls = "https://mp.weixin.qq.com/cgi-bin/searchbiz?action=search_biz&query={query_str}&begin=0&count=3&token={token}&lang=zh_CN&f=json&ajax=1".format(query_str=url_encode_str,token=token)
    headers = {
        'authority': "mp.weixin.qq.com",
        'method': "GET",
        'path': "/cgi-bin/searchbiz?action=search_biz&query={url_str}&begin=0&count=3&token={tokens}&lang=zh_CN&f=json&ajax=1".format(url_str=url_encode_str,tokens=token),
        'scheme': "https",
        'accept': "*/*",
        'accept-encoding': "gzip, deflate, br",
        'accept-language': "zh,en;q=0.9,zh-CN;q=0.8",
        'cookie': cookies_str,
        'referer': "https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit_v2&action=edit&isNew=1&type=10&token={tokens}&lang=zh_CN".format(tokens=token),
        'user-agent': useragent,
        'x-requested-with': "XMLHttpRequest",
    }
    print(urls)
    print(headers)

    response = requests.get(url=urls,headers=headers)
    return response.json()


def get_artile_list(fakeid,token,useragent,cookies_str):
    if isinstance(fakeid,str):
        url_encode_str = quote(fakeid, 'utf-8')
    else:
        raise Exception("传入的公众号名字必须是字符串类型的")
    urls = "https://mp.weixin.qq.com/cgi-bin/appmsg?action=list_ex&fakeid={fakeid}&query=&begin=0&count=4&type=9&need_author_name=1&token={token}&lang=zh_CN&f=json&ajax=1".format(
        fakeid=url_encode_str, token=token)
    headers = {
        'authority': "mp.weixin.qq.com",
        'method': "GET",
        'path': "/cgi-bin/appmsg?action=list_ex&fakeid={fakeid}&query=&begin=0&count=4&type=9&need_author_name=1&token={token}&lang=zh_CN&f=json&ajax=1".format(
            fakeid=url_encode_str, token=token),
        'scheme': "https",
        'accept': "*/*",
        'accept-encoding': "gzip, deflate, br",
        'accept-language': "zh,en;q=0.9,zh-CN;q=0.8",
        'cookie': cookies_str,
        'referer': "https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit_v2&action=edit&isNew=1&type=10&token={tokens}&lang=zh_CN".format(
            tokens=token),
        'user-agent': useragent,
        'x-requested-with': "XMLHttpRequest",
    }
    print(urls)
    print(headers)

    response = requests.get(url=urls, headers=headers)
    return response.json()